package com.tcc.fastline.HttpMethods;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.tcc.fastline.Helpers.ProgressHelper;
import com.tcc.fastline.HttpMethods.Interfaces.IHttpMethods;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class HttpPostTask extends IHttpMethods
{
	private String URL = null;
	private String json = null;
	private HttpEntity httpEntity = null;
	private Integer _codigoResposta;

	public HttpPostTask(String URL, String JSON) {
		this.URL = URL;
		this.json = JSON;
	}

	@Override
	public Integer getStatusHttp()
	{
		return this._codigoResposta;
	}

	@Override
	protected String doInBackground(Object... params)
	{
		String resposta = "";
		HttpClient client = new DefaultHttpClient();
		HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
		HttpResponse response;
		try
		{
			HttpPost post = new HttpPost(URL);
			StringEntity se = new StringEntity(json, "UTF-8");
			post.setHeader(HTTP.CONTENT_TYPE, "application/json");
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
			post.setEntity(se);
			response = client.execute(post);
			if (response != null)
			{
				httpEntity = response.getEntity();
				this._codigoResposta = response.getStatusLine().getStatusCode();
				resposta = EntityUtils.toString(httpEntity);
			}
		}
		catch (Exception e)
		{
			this._codigoResposta = HttpStatus.SC_INTERNAL_SERVER_ERROR;
			resposta = e.getMessage();
		}

		return resposta;
	}

}
